package com.zpp.wedding_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpp.wedding_admin.entity.CameraLocation;
import com.zpp.wedding_admin.entity.LocationLable;
import com.zpp.wedding_admin.entity.vo.LableQuery;
import com.zpp.wedding_admin.service.ICameraLocationService;
import com.zpp.wedding_admin.service.ILocationLableService;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱盼盼
 * @since 2022-12-29
 */
@Api(description = "标签管理")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/label")
public class LocationLableController {
    @Autowired
    private ILocationLableService lableService;

    @Autowired
    private ICameraLocationService cameraLocationService;

    @ApiOperation(value = "获取所有标签")
    @GetMapping("/getAll")
    public R getAll() {
        List<LocationLable> lableList = lableService.list(null);
        return R.ok().data("lableList", lableList);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("getLablePageVo/{currentPage}/{size}")
    public R getLablePageVo(@PathVariable Long currentPage,
                             @PathVariable Long size) {

        Page<LocationLable> page = new Page<LocationLable>(currentPage, size);
        lableService.page(page, null);
        List<LocationLable> lableList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("lableList", lableList).data("total", total);
    }

    @ApiOperation(value = "分页条件查询轮播图列表")
    @PostMapping("getLablePageQuery/{currentPage}/{size}")
    public R getLablePageQuery(@PathVariable Long currentPage,
                                @PathVariable Long size,
                                @RequestBody LableQuery lableQuery) {
        // RequestBody将json转换为实体类
        LambdaQueryWrapper<LocationLable> lqw = new LambdaQueryWrapper<LocationLable>();
        String lableName = lableQuery.getLableName();
        lqw.like(!StringUtils.isEmpty(lableName), LocationLable::getLableName, lableName);
        Page<LocationLable> page = new Page<LocationLable>(currentPage, size);
        lableService.page(page, lqw);
        List<LocationLable> lableList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("lableList", lableList).data("total", total);
    }

    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/deleteAllSelect")
    public R deleteAllSelect(@RequestBody Integer iDs[]) {
        LambdaQueryWrapper<CameraLocation> lqw = new LambdaQueryWrapper<>();
        lqw.in(CameraLocation::getLableId, iDs);
        List<CameraLocation> list = cameraLocationService.list(lqw);
        if ( list.size() > 0 ) {
            return R.ok().data("error_msg", "请先删除带有该标签的拍摄地点");
        }
        boolean b = lableService.deleteAllSelect(iDs);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id删除一个")
    @DeleteMapping("/deleteOneById/{id}")
    public R deleteOneById(@PathVariable Integer id) {
        boolean operation = isOperation(id);
        if ( !operation ) {
            return R.ok().data("error_msg", "请先删除带有该标签的拍摄地点");
        }
        boolean b = lableService.removeById(id);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id获取轮播图信息")
    @GetMapping("/getOneById/{id}")
    public R getOneById(@PathVariable Integer id) {
        LocationLable lable = lableService.getById(id);
        if ( lable != null ) return R.ok().data("lable", lable);
        return R.error();
    }

    @ApiOperation(value = "更新或添加")
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody LocationLable lable) {
        boolean b = lableService.saveOrUpdate(lable);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "模糊查询名称")
    @GetMapping("/searchByName/{lableName}")
    public R searchByName(@PathVariable String lableName) {
        LambdaQueryWrapper<LocationLable> lqw = new LambdaQueryWrapper<LocationLable>();
        lqw.like(LocationLable::getLableName, lableName);
        List<LocationLable> lableList = lableService.list(lqw);
        return R.ok().data("lableList", lableList);
    }

    @ApiOperation(value = "禁用标签")
    @PutMapping("/forbid/{id}")
    public R forbid(@PathVariable Integer id) {
        boolean operation = isOperation(id);
        if ( !operation ) {
            return R.ok().data("error_msg", "请先删除带有该标签的拍摄地点");
        }
        boolean b = lableService.forbid(id);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "解禁标签")
    @PutMapping("/unForbid/{id}")
    public R unForbid(@PathVariable Integer id) {
        boolean b = lableService.unForbid(id);
        if ( b ) return R.ok();
        return R.error();
    }

    public boolean isOperation(Integer id) {
        LambdaQueryWrapper<CameraLocation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CameraLocation::getLableId, id);
        List<CameraLocation> list = cameraLocationService.list(lqw);
        return !(list.size() > 0);
    }
}
